package com.hsxxherp.ation.business.user.center.handler;

import com.hsxxherp.ation.business.user.center.error.AtionUserCenterErrorException;
import com.hsxxherp.ation.common.bean.Response;
import com.hsxxherp.ation.common.error.AtionError;
import com.hsxxherp.ation.common.error.AtionErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@ControllerAdvice
@RestControllerAdvice
public class GlobalExceptionHandler {


    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 基础异常
     */
//    @ExceptionHandler(Exception.class)
//    public Response baseException(Exception exception) {
//        log.warn(exception.getMessage(), exception);
//        return Response.builder().stacktrace(exception.toString()).build();
//    }

    /**
     * 业务异常
     */
    @Order(9999)
    @ExceptionHandler(AtionErrorException.class)
    public Response businessException(AtionErrorException e) {
        AtionError error = e.getError();
        log.warn("【响应错误】:{}", e, e);
        // 如果是500错误标识系统内部错误将错误码替换到 code，messge替换为系统错误，请稍后重试
        if (error.getErrorCode().contains("500")) {
            return Response.error(error).build();
        }
        return Response.error(error).build();
    }
}